package quantri.xemduyetyeucau;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import utils.DateUtils;
import utils.UtilsOnScopes;
import dbutil.ConnectDB;

public class Table  {
	private String stt;
	private String id;
	private String soHC_Madoan;
	private String ngayYeuCau;
	private String userName;
	private String thaoTac;
	private String lydo;
	private String daKhoa;
	private String error;
	private String flag;
	private String isEnableDuyet ="true";
	private String isEnableKhoa ="true";
	private String isEnableMoKhoa ="true";
	private List<Table> tables;
	public Table() {
	}
	
	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getError() {
		return error;
	}


	public void setError(String error) {
		this.error = error;
	}


	public String getThaoTac() {
		return thaoTac;
	}

	public void setThaoTac(String thaoTac) {
		this.thaoTac = thaoTac;
	}

	public String getLydo() {
		return lydo;
	}

	public void setLydo(String lydo) {
		this.lydo = lydo;
	}

	public String getDaKhoa() {
		return daKhoa;
	}

	public void setDaKhoa(String daKhoa) {
		this.daKhoa = daKhoa;
	}

	public String getIsEnableDuyet() {
		return isEnableDuyet;
	}

	public void setIsEnableDuyet(String isEnableDuyet) {
		this.isEnableDuyet = isEnableDuyet;
	}


	public String getIsEnableKhoa() {
		return isEnableKhoa;
	}

	public void setIsEnableKhoa(String isEnableKhoa) {
		this.isEnableKhoa = isEnableKhoa;
	}


	public String getIsEnableMoKhoa() {
		return isEnableMoKhoa;
	}


	public void setIsEnableMoKhoa(String isEnableMoKhoa) {
		this.isEnableMoKhoa = isEnableMoKhoa;
	}


	public String getStt() {
		return stt;
	}
	public void setStt(String stt) {
		this.stt = stt;
	}
	public String getSoHC_Madoan() {
		return soHC_Madoan;
	}
	public void setSoHC_Madoan(String soHC_Madoan) {
		this.soHC_Madoan = soHC_Madoan;
	}
	public String getNgayYeuCau() {
		return ngayYeuCau;
	}
	public void setNgayYeuCau(String ngayYeuCau) {
		this.ngayYeuCau = ngayYeuCau;
	}
	public List<Table> getTables() {
		this.tableList();
		return tables;
	}
	public void setTables(List<Table> tables) {
		this.tables = tables;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	/**************************************
	 * Method name		: tableList
	 * Return type		: List<Table>
	 * Decription		:Dung de hien ra table
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	public List<Table> tableList(){
		this.setError("");
		List<Table> list = new ArrayList<Table>();
		String SQL = "SELECT * FROM phanquyenxoasua WHERE daduyet = '0' ORDER BY " +
				" ngayyeucau DESC,username ASC";
		ArrayList<HashMap<String, Object>> array = ConnectDB
		.getDataTypeList(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			int stt = 0;
			for (int i = 0; i < array.size(); i++) {
				stt = i + 1;
				hash = (HashMap<String, Object>) array.get(i);
				Table table = new Table() ;
				table.setStt(String.valueOf(stt));
				table.setUserName(hash.get("username")== null?"":hash.get("username").toString());
				table.setSoHC_Madoan(hash.get("sohc_idgroup").toString());
				String ngayYeuCau = DateUtils.formatDate(hash.get("ngayyeucau").toString());
				table.setNgayYeuCau(ngayYeuCau);
				table.setDaKhoa((String.valueOf(hash.get("khoa")).equals("0")) ? "Chưa khóa"
							: "Đã khóa");
				table.setId(hash.get("id").toString());
				list.add(table);
			}
		}
		this.setTables(list);
		return list;
	}
	/**************************************
	 * Method name		: xemThongTin
	 * Return type		: String
	 * Decription		:Xem thong tin
	 * Created date		: Jun 15, 2008
	 * Author			: Honglk
	 **************************************/
	public String xemThongTin() {
		HttpServletRequest request = UtilsOnScopes.getRequest();
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		if (flag != null & flag.equalsIgnoreCase("duyet")) {
			this.setError("");
			this.xemThongTinHelp(id, flag);
		} else {
			if (flag.equalsIgnoreCase("moKhoa") && this.isKhoa(id).equals("0")) {
				this.reset1();
				this.setError("Thông tin này đang mở khóa !");
			} else if (flag.equalsIgnoreCase("khoa")
					&& this.isKhoa(id).equals("1")) {
				this.reset1();
				this.setError("Thông tin này đang khóa !");
			} else {
				this.setError("");
				this.xemThongTinHelp(id, flag);
			}

		}
		return "okXemDuyetYeuCau";
	}
	
	/**************************************
	 * Method name		: xemThongTin
	 * Return type		: String
	 * Decription		:Ho tro cho ham xemThongTin
	 * Created date		: Jun 15, 2008
	 * Author			: Honglk
	 **************************************/
	public void xemThongTinHelp(String id,String flag) {
		String SQL = "SELECT * FROM phanquyenxoasua WHERE id='" + id + "'";
		ArrayList<HashMap<String, Object>> array = ConnectDB
				.getDataTypeList(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			hash = (HashMap<String, Object>) array.get(0);
			this.setDaKhoa((String.valueOf(hash.get("khoa")).equals("0")) ? "Chưa khóa"
							: "Đã khóa");
			String xoa = ((String.valueOf(hash.get("xoa")).equals("1"))) ? "Xóa"
					: "";
			String sua = ((String.valueOf(hash.get("sua")).equals("1"))) ? "Sửa"
					: "";
			StringBuilder builder = new StringBuilder();
			if (!(xoa.equals("")) && (!(sua.equals("")))) {
				builder.append(xoa);
				builder.append(",");
				builder.append(sua);
			}else{
				builder.append(xoa);
				builder.append(sua);
			}
			this.setFlag(flag);
			this.setId(hash.get("id").toString());
			this.setThaoTac(builder.toString());
			this.setLydo(hash.get("ghichu").toString());
			String ngayYeuCau = DateUtils.formatDate(hash.get("ngayyeucau")
					.toString());
			this.setNgayYeuCau(ngayYeuCau);
			this.setSoHC_Madoan(hash.get("sohc_idgroup").toString());
			this.setUserName(hash.get("username").toString());
		}
	}
	
	public String isKhoa(String id){
		String result = "";
		String SQL = "SELECT khoa FROM phanquyenxoasua WHERE id='" + id + "'";
		ArrayList<HashMap<String, Object>> array = ConnectDB
		.getDataTypeList(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			hash = (HashMap<String, Object>) array.get(0);
			result = hash.get("khoa").toString();
		}
		return result;
	}
	
	
	/**************************************
	 * Method name		: save
	 * Return type		: String
	 * Decription		:thục hiện lưu.
	 * Created date		: Jun 15, 2008
	 * Author			: Honglk
	 **************************************/
	public String save(){
		if(this.flag != null && this.flag.equalsIgnoreCase("duyet")){
			this.duyet();
		}
		else if(this.flag != null && this.flag.equalsIgnoreCase("moKhoa")){
			this.moKhoa();
		}
		else if(this.flag != null && this.flag.equalsIgnoreCase("khoa")){
			this.khoa();
		}
		else{
			this.setError("Bạn phải chọn 1 trong 3 chức năng bên dưới ! !");
		}
		this.reset1();
		return "okXemDuyetYeuCau";
	}
	
	
	
	/**************************************
	 * Method name		: duyet
	 * Return type		: String
	 * Decription		:Duyet:chp phep nguoi quan ly thao tac tren du lieu
	 * Created date		: Jun 15, 2008
	 * Author			: Honglk
	 **************************************/
	public String duyet() {
		this.setError("");
		try {
			ConnectDB.connectDB();
			String SQL = "UPDATE phanquyenxoasua SET daduyet='1' WHERE id=?";
			PreparedStatement preStat = ConnectDB.connect.prepareStatement(SQL);
			preStat.setString(1, this.id);
			preStat.executeUpdate();
			this.setError("Duyệt thành công !");
		} catch (SQLException e) {
			this.setError("Duyệt không thành công !");
			e.printStackTrace();
		}
		return "okXemDuyetYeuCau";
	}
	
	/**************************************
	 * Method name		: moKhoa
	 * Return type		: String
	 * Decription		:Mo khoa,set lai truong khoa o duoi csdl la 0
	 * Created date		: Jun 15, 2008
	 * Author			: Honglk
	 **************************************/
	public String moKhoa() {
		this.setError("");
		try {
			ConnectDB.connectDB();
			String SQL = "UPDATE phanquyenxoasua SET khoa='0' WHERE id=?";
			PreparedStatement preStat = ConnectDB.connect.prepareStatement(SQL);
			preStat.setString(1, this.id);
			preStat.executeUpdate();
			this.setError("Mở khóa thành công !");
		} catch (SQLException e) {
			this.setError("Mở khóa không thành công !");
			e.printStackTrace();
		}
		return "okXemDuyetYeuCau";
	}
	/**************************************
	 * Method name		: khoa
	 * Return type		: String
	 * Decription		:Khoa,khong cho thao tac tren du lieu
	 * Created date		: Jun 15, 2008
	 * Author			: Phonglt
	 **************************************/
	public String khoa() {
		this.setError("");
		try {
			ConnectDB.connectDB();
			String SQL = "UPDATE phanquyenxoasua SET khoa='1' WHERE id=?";
			PreparedStatement preStat = ConnectDB.connect.prepareStatement(SQL);
			preStat.setString(1, this.id);
			preStat.executeUpdate();
			this.setError("Khóa thành công !");
		} catch (SQLException e) {
			this.setError("Khóa không thành công !");
			e.printStackTrace();
		}
		return "okXemDuyetYeuCau";
	}
	
	
	/**************************************
	 * Method name		: reset
	 * Return type		: String
	 * Decription		:reset lai
	 * Created date		: Jun 15, 2008
	 * Author			: Honglk
	 **************************************/
	public String reset(){
		UtilsOnScopes.getSession().removeAttribute("table");
		this.setDaKhoa("");
		this.setIsEnableDuyet("true");
		this.setIsEnableKhoa("true");
		this.setIsEnableMoKhoa("true");
		this.setLydo("");
		this.setNgayYeuCau("");
		this.setSoHC_Madoan("");
		this.setThaoTac("");
		this.setUserName("");
		this.setError("");
		this.setId("");
		return "okXemDuyetYeuCau";
	}
	public void reset1(){
		this.setDaKhoa("");
		this.setLydo("");
		this.setNgayYeuCau("");
		this.setSoHC_Madoan("");
		this.setThaoTac("");
		this.setUserName("");
		this.setId("");
	}
}
